package com.carrie.mdg.myviewtext.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.carrie.mdg.myviewtext.R;
import com.carrie.mdg.myviewtext.activity.NewsTestActivity;
import com.carrie.mdg.myviewtext.entity.News;
import com.carrie.mdg.myviewtext.fragments.NewsContentFragment;

import java.util.List;

/**
 * Created by mdg on 2017/3/31.
 */

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
    private List<News> mNews;

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.news_title);
        }
    }

    public NewsAdapter(List<News> mNews) {
        this.mNews = mNews;
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_title_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                News news = mNews.get(holder.getAdapterPosition());
                NewsTestActivity activity = (NewsTestActivity) parent.getContext();
                NewsContentFragment fragment = (NewsContentFragment) activity.getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
                fragment.refresh(news.getTitle(), news.getContent());
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        News news = mNews.get(position);
        holder.title.setText(news.getTitle());
    }

    @Override
    public int getItemCount() {
        return mNews.size();
    }


}
